﻿using System;
using WeightConverterNamespace;

namespace Presentation
{
    public class Presenter
    {
        IView view;
        ILogger logger;
        WeightType fromType;       
        WeightConverter converter;
        double fromValue;

        public Presenter(IView view, ILogger logger)
        {
            this.view = view;
            this.logger = logger;
            view.OnCalculateClick += CalculateClick;
            view.SelectedIndexChanged += SelectedIndexChange;
            view.OnReadLogButtonClick += ReadLogMenuItemClick;
            view.OnDeleteLogButtonClick += DeleteLogMenuItemClick;
            view.OnTxtLoggerMenuItemClick += TxtLoggerMenuItemClick;
            view.OnHTMLLoggerMenuItemClick += HTMLLoggerMenuItemClick;
            view.LogHandler += SetLogger;
            converter = new WeightConverter();
        }
        private void SetLogger(ILogger log)
        {
            logger = log;
        }
        private void SelectedIndexChange()
        {
            logger.WriteLog("Method: SelectedIndexChange()");
            view.TBoxKgReadOnly = true;
            view.TBoxPoundReadOnly = true;
            view.TBoxOunceReadOnly = true;
            view.TBoxDramReadOnly = true;
            view.TBoxGrainReadOnly = true;
            view.ToKg = "0,0";
            view.ToPound = "0,0";
            view.ToOunce = "0,0";
            view.ToDram = "0,0";
            view.ToGrain = "0,0";
            switch (view.FromType)
            {
                case 0: view.TBoxKgReadOnly = false;
                    break;
                case 1: view.TBoxPoundReadOnly = false;
                    break;
                case 2: view.TBoxOunceReadOnly = false;
                    break;
                case 3: view.TBoxDramReadOnly = false;
                    break;
                case 4: view.TBoxGrainReadOnly = false;
                    break;
            }
            logger.WriteLog("    Choosen type = " + ((WeightType)view.FromType).ToString());
        }
        private void CalculateClick()
        {
            logger.WriteLog("Method: CalculateClick()");
            if (!ConvertInputValueToDouble())
            {
                logger.WriteLog("    Input value is not double!");
                return;
            }
            logger.WriteLog("    Input value = " + fromValue.ToString());
            try
            {
                var knownWeight = new Weight(fromType, fromValue);
                var newWeightInKg = new Weight(WeightType.kg, 0.0);
                var newWeightInPound = new Weight(WeightType.pound, 0.0);
                var newWeightInOunce = new Weight(WeightType.ounce, 0.0);
                var newWeightInDram = new Weight(WeightType.dram, 0.0);
                var newWeightInGrain = new Weight(WeightType.grain, 0.0);

                converter.Convert(knownWeight, ref newWeightInKg);
                converter.Convert(knownWeight, ref newWeightInPound);
                converter.Convert(knownWeight, ref newWeightInOunce);
                converter.Convert(knownWeight, ref newWeightInDram);
                converter.Convert(knownWeight, ref newWeightInGrain);

                view.ToKg = Convert.ToString(newWeightInKg.Quantity);
                view.ToPound = Convert.ToString(newWeightInPound.Quantity);
                view.ToOunce = Convert.ToString(newWeightInOunce.Quantity);
                view.ToDram = Convert.ToString(newWeightInDram.Quantity);
                view.ToGrain = Convert.ToString(newWeightInGrain.Quantity);
                view.ErrorMessage = ErrorMessage.NoError;
            }
            catch (NegativeAmountException)
            {
                view.ErrorMessage = ErrorMessage.NegativeWeight;
                logger.WriteLog("    Input value is negative!");
            }
        }       
        private bool ConvertInputValueToDouble()
        {
            if (!Double.TryParse(view.FromValue, out fromValue))    
            {
                view.ErrorMessage = ErrorMessage.IncorrectValue;
                return false;
            }
            fromType = (WeightType)view.FromType;
            view.ErrorMessage = ErrorMessage.NoError;
            return true;                      
        }
        private void ReadLogMenuItemClick()
        {
            try
            {
                System.Diagnostics.Process.Start(logger.FileName);
                logger.WriteLog("Method: ReadLogMenuItemClick()");
                view.ErrorMessage = ErrorMessage.NoError;
            }
            catch (Exception)
            {
                view.ErrorMessage = ErrorMessage.NoLogFile;
            }
        }
    
        private void DeleteLogMenuItemClick()
        {          
            logger.DeleteLog();
            logger.WriteLog("Method: DeleteLogMenuItemClick()");
        }
        private void TxtLoggerMenuItemClick()
        {
            view.TxtLoggerMenuItemChecked = true;
            view.HTMLLoggerMenuItemChecked = false;
            logger.WriteLog("Method: TxtLoggerMenuItemClick()");
        }
        private void HTMLLoggerMenuItemClick()
        {
            view.TxtLoggerMenuItemChecked = false;
            view.HTMLLoggerMenuItemChecked = true;
            logger.WriteLog("Method: HTMLLoggerMenuItemClick()");
        }
    }
}